package cn.zjyj.module.zjyj.controller.admin.riskassessmentrecord;

import cn.zjyj.module.zjyj.controller.admin.riskidentifytarget.vo.RiskAssessmentSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.riskassessmentrecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentrecord.RiskAssessmentRecordDO;
import cn.zjyj.module.zjyj.service.riskassessmentrecord.RiskAssessmentRecordService;

@Tag(name = "管理后台 - 风险评估记录")
@RestController
@RequestMapping("/zjyj/risk-assessment-record")
@Validated
public class RiskAssessmentRecordController {

    @Resource
    private RiskAssessmentRecordService riskAssessmentRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建风险评估记录")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:create')")
    public CommonResult<Long> createRiskAssessmentRecord(@Valid @RequestBody RiskAssessmentRecordSaveReqVO createReqVO) {
        return success(riskAssessmentRecordService.createRiskAssessmentRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险评估记录")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:update')")
    public CommonResult<Boolean> updateRiskAssessmentRecord(@Valid @RequestBody RiskAssessmentRecordSaveReqVO updateReqVO) {
        riskAssessmentRecordService.updateRiskAssessmentRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险评估记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:delete')")
    public CommonResult<Boolean> deleteRiskAssessmentRecord(@RequestParam("id") Long id) {
        riskAssessmentRecordService.deleteRiskAssessmentRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险评估记录")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:delete')")
    public CommonResult<Boolean> deleteRiskAssessmentRecordList(@RequestParam("ids") List<Long> ids) {
        riskAssessmentRecordService.deleteRiskAssessmentRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险评估记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:query')")
    public CommonResult<RiskAssessmentRecordRespVO> getRiskAssessmentRecord(@RequestParam("id") Long id) {
        RiskAssessmentRecordDO riskAssessmentRecord = riskAssessmentRecordService.getRiskAssessmentRecord(id);
        return success(BeanUtils.toBean(riskAssessmentRecord, RiskAssessmentRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险评估记录分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:query')")
    public CommonResult<PageResult<RiskAssessmentRecordRespVO>> getRiskAssessmentRecordPage(@Valid RiskAssessmentRecordPageReqVO pageReqVO) {
        PageResult<RiskAssessmentRecordDO> pageResult = riskAssessmentRecordService.getRiskAssessmentRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskAssessmentRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险评估记录 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskAssessmentRecordExcel(@Valid RiskAssessmentRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskAssessmentRecordDO> list = riskAssessmentRecordService.getRiskAssessmentRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险评估记录.xls", "数据", RiskAssessmentRecordRespVO.class,
                        BeanUtils.toBean(list, RiskAssessmentRecordRespVO.class));
    }

    @PostMapping("/submitAssessment")
    @Operation(summary = "提交风险评估")
    public CommonResult<Boolean> submitAssessment(@Valid @RequestBody RiskAssessmentSaveReqVO createReqVO) {
        riskAssessmentRecordService.submitAssessment(createReqVO);
        return success(true);
    }

}